package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.HomeFrg.HomeFrg_Data;
import com.banzhi.shop.model.CallBack.HomeFrgModelCallBack;
import com.banzhi.shop.model.IHomeFrgModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by HT on 2017/5/3.
 * 首页
 */

public class HomeFrgModelImpl implements IHomeFrgModel {
    //获取首页信息
    @Override
    public void getData( String page,final HomeFrgModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.recommendUrl);
        //page
        params.addBodyParameter("page", page);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.e("HomeFrgModelImpl--2--", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.d("HomeFrgModelImpl--2--", result.toString());
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        HomeFrg_Data data = gson.fromJson(result, HomeFrg_Data.class);
                        callBack.getDataSuccess(data);
                    } else {
                        callBack.getDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("HomeFrgModelImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(3)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //根据ID获取首页信息
    @Override
    public void getDataforID(String id, String page, final HomeFrgModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.shouyeUrl);
        params.addBodyParameter("id", id);
        params.addBodyParameter("page", page);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("jiejie","----" + params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtils.d("jiejie","----" + result);
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        HomeFrg_Data data = gson.fromJson(result, HomeFrg_Data.class);
                        callBack.getDataSuccess(data);
                    } else {
                        callBack.getDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("HomeFrgModelImpl2", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
//                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * @param goods_id
     * @param callBack 将商品添加到店铺
     */
    @Override
    public void AddDianpu(String goods_id, final HomeFrgModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.addShopGoodsUrl);
        params.addBodyParameter("goods_id", goods_id);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("HomeFrgModelImpl3", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        callBack.AddDianpuSuccess(jsonobject);
                    } else {
                        callBack.AddDianpuError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("HomeFrgModelImpl3", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
               ToastUtils.makeToastShort("网络加载失败,请稍后再试(22)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * @param goods_id   这是商品id
     * @param brandid    这是品牌id
     *                   两个只能传一个
     * @param callBack
     * 删除商品
     */
    @Override
    public void deleteGoods(String goods_id, String brandid,final HomeFrgModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.deleleShopUrl);
        if(goods_id!=null){
            params.addBodyParameter("goods_id", goods_id);
        }
        if(brandid!=null){
            params.addBodyParameter("brandid", brandid);
        }
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("ManagementImpl2222222", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        callBack.deleteSuccess(jsonobject);
                    } else {
                        callBack.deleteError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("ManagementImpl22222", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(23)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
